<?php
declare (strict_types = 1);

namespace app\article\controller;

use app\article\model\Admian;
use app\article\model\City;
use app\Request;
use think\exception\ValidateException;
class Index
{
    public function index()
    {
        return '您好！这是一个[article]示例应用';
    }
    public function show(){
        $data=Admian::with(['city','province','area'])->order('id','desc')->paginate(5);
//        dd($data->toArray());
        return view('show',['data'=>$data]);
    }
    public function add(Request $request){
        $error='';
        $citvs= \app\admin\model\City::where('pid',0)->select();
        if($request->isPost()){
            $data=$request->post();
//            dd($data);
            try {
                $file = request()->file('img');
                $data['img']=$file;
                validate(\app\article\validate\Admian::class)->check($data);
                $data['img'] = \think\facade\Filesystem::disk('public')->putFile( 'topic', $file);

                $path=public_path().'storage/'.$data['img'];
                $image = \think\Image::open($path);
//                dd($path);
                $geshi=pathinfo($path);
//                dd($geshi);
                $pjie=$geshi['dirname'].'/'.$geshi['filename'].'_100.'.$geshi['extension'];
//                dd($pjie);

// 按照原图的比例生成一个最大为150*150的缩略图并保存为thumb.png
                $image->thumb(100,100,\think\Image::THUMB_SOUTHEAST)

                    ->save($pjie);
                $res=Admian::create($data);
                if($res){
                    return redirect(url('index/show')->build());
                }
            } catch (ValidateException $e) {
                // 验证失败 输出错误信息
                $error=$e->getError();
            } catch (\Exception $e){
                $error=$e->getMessage();
            }
        }
        return view('add',compact('error','citvs'));
    }
    public function lianD(Request $request){
        $pid=$request->post('pid');
        $citvs= \app\admin\model\City::where('pid',$pid)->select();
        return $citvs;
    }
}
